<?php

@session_start();
require_once 'shared-functions.php';
require_once 'session.php';
require('fpdf.php');

 $link = connect_db();

 $query = "SELECT `User`.`FirstName`,
                     `User`.`MiddleName`,
                     `User`.`LastName`,
                     `User`.`UserId`,
                     `User`.`EmailAddress`,
                     `Student`.`Address`,
                     `Student`.`City`,
                     `Student`.`Region`,
                     `Student`.`PostalAddress`,
                     `Student`.`Country`,
                     `FeeLedger`.`ledgerID`,
                     `FeeLedger`.`Amount`,
                     `FeeLedger`.`Comment`,
                     `feeTypes`.`description`,
                     `PaymentMethod`.`Description`
                FROM `FeeLedger`
                LEFT JOIN `Student` ON `FeeLedger`.`StudentID` = `Student`.`ID`
                LEFT JOIN `User` ON `Student`.`ID` = `User`.`UserID`
                LEFT JOIN `feeTypes` ON `FeeLedger`.`Type` = `feeTypes`.`type`
                LEFT JOIN `PaymentMethod` ON `FeeLedger`.`PaymentMethod` = `PaymentMethod`.`PaymentID`
                WHERE `FeeLedger`.`StudentID`='".$_SESSION['studentID']."' AND
                      `FeeLedger`.`Date`='".$_SESSION['dateTime']."';";

    $result = mysql_query($query, $link);
    $transactionDetails = mysql_fetch_array($result, MYSQL_BOTH);

    $query = "SELECT SUM(`amount`) FROM `FeeLedger` WHERE `StudentID`='".$_SESSION['studentID']."';";
    $result = mysql_query($query,$link);
    $currentBalance = mysql_fetch_array($result,MYSQL_BOTH);
	
	// check whether print-receipt is for a fee, otherwise it will be for a payment
	$isFee = false;
	if (isset($_GET['type']) && $_GET['type'] == "fee")
		$isFee = true;

    $pdf=new FPDF();
    $pdf->AddPage();
    //Header
    $pdf->SetFont('Arial','B',24);
    $pdf->SetTextColor(255,255,255);
    $pdf->SetFillColor(48,68,98);
    $pdf->SetDrawColor(48,68,98);
    $pdf->Cell(40,10,'Receipt',1,0,'L',true);
    $pdf->SetFontSize(12);
    $pdf->Cell(100,10,'',1,0,'L',true);
    $pdf->Cell(50,5,'#'.$transactionDetails['ledgerID'],1,2,'R',true);
    $pdf->Cell(50,5,$_SESSION['dateTime'],1,1,'R',true);
    $pdf->Cell(10,3,"",0,1);

//Student Details
    $pdf->SetTextColor(48,68,98);
    $pdf->SetFontSize(18);
    $pdf->Cell(100,10,"Student Information",0,1,'L');
    $pdf->SetTextColor(0,0,0);
    $pdf->SetFont('Arial','',12);
    $pdf->Cell(90,6,$transactionDetails['UserId'],0,1,'L');
    $pdf->Cell(90,6,str_replace('  ',' ',$transactionDetails['FirstName']." ".
                    $transactionDetails['MiddleName']." ".
                    $transactionDetails['LastName']),0,1,'L');
    if($transactionDetails['EmailAddress']!="")
    {
        $pdf->Cell(90,6,$transactionDetails['EmailAddress'],0,1,'L');
    }
    $pdf->Cell(90,6,$transactionDetails['Address'],0,1,'L');
    $pdf->Cell(90,6,str_replace("  "," ",$transactionDetails['City'].", ".
                    $transactionDetails['Region']." ".
                    $transactionDetails['PostalAddress']),0,1,'L');
    $pdf->Cell(90,6,$transactionDetails['Country'],0,1,'L');
    $pdf->Cell(10,10,'',0,1);

//Payment Details
    $pdf->SetTextColor(48,68,98);
    $pdf->SetFont('Arial','B',18);
	if ($isFee)
		$pdf->Cell(100,10,"Fee Details",0,1,'L');
	else
		$pdf->Cell(100,10,"Payment Details",0,1,'L');
    $pdf->SetTextColor(0,0,0);
	if ($isFee == false)
	{
		$pdf->SetFont('Arial','B',10);
		$pdf->Cell(30,6,'Payment Method: ',0,0,'L');
		$pdf->SetFont('Arial','',10);
		$pdf->Cell(60,6,$transactionDetails['Description'],0,1,'L');
	}
    $pdf->SetFont('Arial','B',10);
	if ($isFee)
		$pdf->Cell(33,6,'Fee Category: ',0,0,'L');
	else
		$pdf->Cell(33,6,'Payment Category: ',0,0,'L');
    $pdf->SetFont('Arial','',10);
    $pdf->Cell(40,6,$transactionDetails['description'],0,1,'L');
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(16,6,'Amount: ',0,0,'L');
    $pdf->SetFont('Arial','',10);
    $pdf->Cell(40,6,$transactionDetails['Amount'],0,1,'L');
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(45,6,'Current Account Balance: ',0,0,'L');
    $pdf->SetFont('Arial','',10);
    $pdf->Cell(40,6,$currentBalance[0],0,1,'L');
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(18,6,'Comment: ',0,0,'L');
    $pdf->SetFont('Arial','',10);
    $pdf->MultiCell(90,6,$transactionDetails['Comment'],0,'L');
    $pdf->Cell(10,10,'',0,1);



//Signature Block
    $pdf->SetTextColor(48,68,98);
    $pdf->SetFont('Arial','B',18);
    $pdf->Cell(100,10,"Signatures",0,1,'L');
    $pdf->SetTextColor(0,0,0);
    $pdf->SetDrawColor(0,0,0);
    $pdf->SetFont('Arial','',10);
    $pdf->Cell(190,15,"",0,1);
    $pdf->Cell(70,6,"Student Name (Print)",'T',0,'L');
    $pdf->Cell(5,6,"",0,0);
    $pdf->Cell(70,6,"Student Signature",'T',0,'L');
    $pdf->Cell(5,6,"",0,0);
    $pdf->Cell(40,6,"Date",'T',0,'L');
    $pdf->Cell(190,20,"",0,1);
    $pdf->Cell(70,6,"Registrar Name (Print)",'T',0,'L');
    $pdf->Cell(5,6,"",0,0);
    $pdf->Cell(70,6,"Registrar Signature",'T',0,'L');
    $pdf->Cell(5,6,"",0,0);
    $pdf->Cell(40,6,"Date",'T',0,'L');

    //Draw PDF;
    $pdf->Output();


?>
